package com.letv.core.sysletvplayer.utils;

import java.util.Formatter;
import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.media.ThumbnailUtils;
import android.provider.MediaStore.Images;
import android.view.WindowManager;

public class Tools {
	
	/**
	 * 得到屏幕宽度
	 * */
	public static int getScreenWidth(Context context) {
		return ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE))
				.getDefaultDisplay().getWidth();
	}

	/**
	 * 得到屏幕高度
	 * */
	public static int getScreenHeight(Context context) {
		return ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE))
				.getDefaultDisplay().getHeight();
	}
	
	/**
	 * 设置为横屏
	 * */
	public static void screenLandscape(Activity activity) {
		activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
	}

	/**
	 * 设置为竖屏
	 * */
	public static void screenPortrait(Activity activity) {
		activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
	}
	
	/**
	 * 判断是否是横屏
	 * */
	public static boolean isLandscape(Activity activity) {

		int t = activity.getResources().getConfiguration().orientation;
		return t == Configuration.ORIENTATION_LANDSCAPE;

	}
	
	public static String getStringTwo(String strIn) {
		String strResult;
		if (strIn.length() >= 2) {
			strResult = strIn;
		} else {
			strResult = "0".concat(String.valueOf(String.valueOf(strIn)));
		}
		return strResult;
	}
	
	/**
	 * 格式化时间字符串
	 * 
	 * @param timeMs
	 *            毫秒
	 * @return 返回格式00:00:00
	 */
	public static String stringForTime(long timeMs) {

		StringBuilder formatBuilder = new StringBuilder();
		Formatter formatter = new Formatter(formatBuilder, Locale.getDefault());

		try {
			int totalSeconds = (int) timeMs / 1000;

			int seconds = totalSeconds % 60;
			int minutes = (totalSeconds / 60) % 60;
			int hours = totalSeconds / 3600;

			formatBuilder.setLength(0);

			if (hours > 0) {
				return formatter.format("%d:%02d:%02d", hours, minutes, seconds).toString();
			} else {
				return formatter.format("%02d:%02d", minutes, seconds).toString();
			}
		} finally {
			formatter.close();
		}
	}
	
	/**
	 *  获得视频缩略图
	 */
	public static Bitmap createVideoThumbnail(String path) {
		Bitmap bitmap = null;
		try {
			bitmap = ThumbnailUtils.createVideoThumbnail(path,
					Images.Thumbnails.MINI_KIND);
		} catch (IllegalArgumentException ex) {
			return null;
		} catch (RuntimeException ex) {
			return null;
		} catch (OutOfMemoryError ex) {
			System.gc();
			return null;
		}
		return bitmap;
	}
	
	/**
	 * 获取当前程序的版本号
	 */
	public static int getVersionCode(Context context) {
		try {
			PackageManager manager = context.getPackageManager();
			PackageInfo info = manager.getPackageInfo(context.getPackageName(),
					0);
			return info.versionCode;
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
	}
	
	/**
	 * 获取当前程序的版本号
	 */
	public static String getVersionName(Context context) {
		try {
			PackageManager manager = context.getPackageManager();
			PackageInfo info = manager.getPackageInfo(context.getPackageName(),
					0);
			return info.versionName;
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}
	
}
